package net.pm.core.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.pm.common.dao.GenericDao;
import net.pm.common.service.impl.GenericServicerImpl;
import net.pm.common.toolkit.DevConstant;
import net.pm.common.toolkit.PMTool;
import net.pm.common.toolkit.Pager;
import net.pm.common.toolkit.date.DateUtils;
import net.pm.core.entities.db.Materiallog;
import net.pm.core.entities.db.Orderinfo;
import net.pm.core.entities.db.Pmuser;
import net.pm.core.entities.db.Product;
import net.pm.core.service.GuestServicer;
import net.pm.core.service.MateriallogServicer;
import net.pm.core.service.OrderServicer;
import net.pm.core.service.ProductServicer;

import org.springframework.stereotype.Service;

@Service("OrderServicer")
public class OrderServicerImpl extends GenericServicerImpl<Orderinfo, Long>
		implements OrderServicer {
	
	@Resource(name="GuestServicer")
	private GuestServicer guestServicer;
	@Resource(name="ProductServicer")
	private ProductServicer productServicer;
	@Resource(name="MateriallogServicer")
	private MateriallogServicer materiallogServicer;
	
	@Resource(name="OrderDao")
	@Override
	public void setGenericDao(GenericDao<Orderinfo, Long> genericDao) {
		super.genericDao = genericDao;
	}

	@Override
	public Long createOrder(Orderinfo order) {
		//添加客户信息
		guestServicer.create(order.getGuestinfo());
		//添加产品基础信息
		order.getProduct().setStatus(Product.STATUS_PRODUCE);
		productServicer.create(order.getProduct());
		
		//setter 一些默认属性
		order.setIsDelete(false);     												//已经删除默认为false
		order.setOrderNo(PMTool.generateUniqueno(DevConstant.ORDERNO_PREFIX));		//生成订单号
		order.setStatus(Orderinfo.STATUS_WAIT);										//设置生产订单状态为: 等候生产
		order.setOrderTime(DateUtils.obtainCurrentDate());							//设置下单时间
		
		if(PMTool.isNotEmpty(super.create(order))){
			return OrderServicer.create_order_success;
		}
		
		return OrderServicer.create_order_failed;
	}

	@Override
	public Pager<Orderinfo> findOrderPagerByOwner(Short statusKey,Date startdate,Date enddate,Integer pagesize,
			Integer pageno) {
		final List<Object> args = new ArrayList<Object>();
		final StringBuilder builder = new StringBuilder(" from Orderinfo orderinfo where orderinfo.isDelete = ? ");
		args.add(false);
		
		if(PMTool.isNotEmpty(startdate)){
			builder.append(" and orderinfo.orderTime >= ? ");
			args.add(startdate);
		}
		if(PMTool.isNotEmpty(enddate)){
			builder.append(" and orderinfo.orderTime <= ? ");
			args.add(enddate);
		}
		
		Short[] statues = obtainStatuses(statusKey);
		if(PMTool.isEmpty(statues)){
			builder.append(" and orderinfo.status = ? ");
			args.add(statusKey);
		}else{
			builder.append(" and orderinfo.status in (-1 ");
			for(Short s : statues){
				builder.append(",").append(" ? ");
				args.add(s);
			}
			builder.append(" ) "); 
		}
		builder.append(" order by orderinfo.orderTime desc  ");
		
		return super.findPagerByJpql(pagesize, pageno, builder, args.toArray());
	}
	
	private Short[] obtainStatuses(Short statusKey){
		return OrderServicer.statusesMap.get(statusKey);
	}

	@Override
	public List<Orderinfo> findOrdersOfStatusProduce(Integer deptId, Long userId) {
		final StringBuilder builder = new StringBuilder(" from Orderinfo orderinfo where orderinfo.product.status = ?  ");
		final List<Object> args = new ArrayList<Object>();
		args.add(Product.STATUS_PRODUCE);
		
		builder.append(" and orderinfo.status not in (?,?,?) ");
		args.add(Orderinfo.STATUS_WAIT);
		args.add(Orderinfo.STATUS_FAILED);
		args.add(Orderinfo.STATUS_CANCEL);
		
		if(PMTool.isNotEmpty(userId)){
			builder.append(" and orderinfo.product.produceUser.id = ? ");
			args.add(userId);
		}else{
			builder.append(" and orderinfo.product.produceUser.pmdept.id = ? ");
			args.add(deptId);
		}
		
		List<Orderinfo> orders = super.findEntitiesByJpql(builder, args.toArray());
		for(Orderinfo order : orders){
			List<Materiallog> materiallogs = materiallogServicer.findMaterialogTop5ByProductId(order.getProduct().getId());
			order.getProduct().setMateriallogs(materiallogs);
		}
		return orders;
	}

	@Override
	public void intoProduce(Long orderId,Long userId) {
		Orderinfo order = super.findEntityById(orderId);
		order.setStatus( Orderinfo.STATUS_PRODUCE);
		order.getProduct().setProduceUser(new Pmuser(userId));
		super.modify(order);
	}

	@Override
	public void cancelOrder(Long orderId) {
		final StringBuilder builder = new StringBuilder(" update Orderinfo orderinfo set orderinfo.status = ? where orderinfo.id = ? ");
		super.modifyByJpql(builder,Orderinfo.STATUS_CANCEL,orderId);
	}

	@Override
	public void removeOrder(Long orderId) {
		final StringBuilder builder = new StringBuilder(" update Orderinfo orderinfo set orderinfo.isDelete = ? where orderinfo.id = ? ");
		super.modifyByJpql(builder, true,orderId);
	}


}
